package com.zhongxi.guoyuan.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.application.MyApplication;
import com.zhongxi.guoyuan.utils.Constants;
import com.zhongxi.guoyuan.view.MainHomeActivity;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler{
	
	private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";
	
    private IWXAPI api;
	private TextView tvTitle;
	private ImageView ivBack;

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pay_result);
		tvTitle = (TextView) findViewById(R.id.tvTitle);
		tvTitle.setText("支付成功");
		ivBack = (ImageView) findViewById(R.id.ivBack);

		ivBack.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(WXPayEntryActivity.this, MainHomeActivity.class);
				startActivity(intent);
				finish();
			}
		});

    	api = WXAPIFactory.createWXAPI(this, Constants.APP_ID);
        api.handleIntent(getIntent(), this);

    }

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		setIntent(intent);
        api.handleIntent(intent, this);
	}

	@Override
	public void onReq(BaseReq req) {
	}

	@Override
	public void onResp(BaseResp resp) {
		int code= resp.errCode;
		Log.i("base", "resp:"+resp.toString());
		if(code == -2){
			tvTitle.setText("支付失败");
			Toast.makeText(WXPayEntryActivity.this, "支付取消！", Toast.LENGTH_LONG).show();
			if(null!= MyApplication.getInstance().getPay()&&!"".equals(MyApplication.getInstance().getPay())){
				if(MyApplication.getInstance().getPay().equals("beauty")) {
					showFailPopWindow();
				}else if(MyApplication.getInstance().getPay().equals("reward")){
					showFailRewardWindow();
				}else{
					showFailSuperWindow();
				}
			}
		}else if(code == -1){
			tvTitle.setText("支付失败");
			Toast.makeText(WXPayEntryActivity.this,"支付失败！",Toast.LENGTH_LONG).show();
			if(null!= MyApplication.getInstance().getPay()&&!"".equals(MyApplication.getInstance().getPay())){
				if(MyApplication.getInstance().getPay().equals("beauty")) {
					showFailPopWindow();
				}else if(MyApplication.getInstance().getPay().equals("reward")){
					showFailRewardWindow();
				}else{
					showFailSuperWindow();
				}
			}
		}else{
			Toast.makeText(WXPayEntryActivity.this,"支付成功！",Toast.LENGTH_LONG).show();
			if(null!= MyApplication.getInstance().getPay()&&!"".equals(MyApplication.getInstance().getPay())){
				if(MyApplication.getInstance().getPay().equals("beauty")){
					showPopupWindow();
				}else if(MyApplication.getInstance().getPay().equals("reward")){
					showRewardWindow();
				}else{
					showSuperWindow();
				}
			}
		}
	}

	/**
	 * 返银商品支付失败
	 */
	private void showFailRewardWindow() {
		LayoutInflater inflater = LayoutInflater.from(this);
		LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.dialog_pay_fail, null);
		final AlertDialog dialog = new AlertDialog.Builder(WXPayEntryActivity.this).create();
		dialog.setCanceledOnTouchOutside(false);
		dialog.show();
		dialog.getWindow().setContentView(layout);
		ImageView ivStep = (ImageView) layout.findViewById(R.id.ivStep);
		ivStep.setImageResource(R.mipmap.reward_fail);
		TextView btSure = (TextView) layout.findViewById(R.id.btSure);
		btSure.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				dialog.dismiss();
				Intent intent = new Intent(WXPayEntryActivity.this, MainHomeActivity.class);
				startActivity(intent);
				finish();
			}
		});
	}

	/**
	 * 返银商品支付成功
	 */
	private void showRewardWindow() {
		LayoutInflater inflater = LayoutInflater.from(this);
		LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.dialog_pay_success, null);
		final AlertDialog dialog = new AlertDialog.Builder(this).create();
		dialog.setCanceledOnTouchOutside(false);
		dialog.show();
		dialog.getWindow().setContentView(layout);
		TextView tvContent = (TextView) layout.findViewById(R.id.tvContent);
		ImageView ivStep = (ImageView) layout.findViewById(R.id.ivStep);
		ivStep.setImageResource(R.mipmap.reward_success);
		tvContent.setText("支付已成功，待您确认此项目，点击“已操作”后，即可获得:");
		layout.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				dialog.dismiss();
				Intent intent = new Intent(WXPayEntryActivity.this, MainHomeActivity.class);
				startActivity(intent);
				finish();
			}
		});
	}

	/**
	 *权益会员支付失败
	 */
	private void showFailSuperWindow() {
		Toast.makeText(WXPayEntryActivity.this, "权益会员支付失败！！！", Toast.LENGTH_SHORT).show();
		LayoutInflater inflater = LayoutInflater.from(this);
		LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.dialog_superman_fail, null);
		final AlertDialog dialog = new AlertDialog.Builder(this).create();
		dialog.setCanceledOnTouchOutside(false);
		dialog.show();
		dialog.getWindow().setContentView(layout);
		layout.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				dialog.dismiss();
				Intent intent = new Intent(WXPayEntryActivity.this, MainHomeActivity.class);
				startActivity(intent);
				finish();
			}
		});
	}

	/**
	 * 权益会员支付成功
	 */
	private void showSuperWindow() {
		LayoutInflater inflater = LayoutInflater.from(this);
		LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.dialog_superman_success, null);
		final AlertDialog dialog = new AlertDialog.Builder(this).create();
		dialog.setCanceledOnTouchOutside(false);
		dialog.show();
		dialog.getWindow().setContentView(layout);
		layout.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				dialog.dismiss();
				Intent intent = new Intent(WXPayEntryActivity.this, MainHomeActivity.class);
				startActivity(intent);
				finish();
			}
		});
	}

	/**
	 * 美币美容院支付成功
	 */
	private void showPopupWindow() {
		LayoutInflater inflater = LayoutInflater.from(this);
		LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.dialog_pay_success, null);
		final AlertDialog dialog = new AlertDialog.Builder(WXPayEntryActivity.this).create();
		dialog.setCanceledOnTouchOutside(false);
		dialog.show();
		dialog.getWindow().setContentView(layout);
		TextView btSure = (TextView) layout.findViewById(R.id.btSure);
		btSure.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				dialog.dismiss();
				Intent intent = new Intent(WXPayEntryActivity.this, MainHomeActivity.class);
				startActivity(intent);
				finish();
			}
		});
	}

	/**
	 * 美币美容院支付失败
	 */
	private void showFailPopWindow() {
		LayoutInflater inflater = LayoutInflater.from(this);
		LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.dialog_pay_fail, null);
		final AlertDialog dialog = new AlertDialog.Builder(WXPayEntryActivity.this).create();
		dialog.setCanceledOnTouchOutside(false);
		dialog.show();
		dialog.getWindow().setContentView(layout);
		TextView btSure = (TextView) layout.findViewById(R.id.btSure);
		btSure.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				dialog.dismiss();
				Intent intent = new Intent(WXPayEntryActivity.this, MainHomeActivity.class);
				startActivity(intent);
				finish();
			}
		});
	}
}